<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>增加银行卡</title>
		<script type="text/javascript" src="../../js/jquery-3.2.1.min.js" ></script>
		<script type="text/javascript" src="../../js/common.js" ></script>
	</head>
	<body >
		
		<p >银行卡的新增</p>
		<table border="1" cellpadding="0" cellspacing="0"  width="500">
			
			<tr>
				<td>银行名称</td>
				<td>
					<select name="bankId" id="bankId">
						
					</select>
				</td>
			</tr>
			<tr>
				<td>卡号</td>
				<td>
					<input name="cardNo" value="1234567812345678"/>
				</td>
			</tr>
			
			<tr>
				<td>有无芯片</td>
				<td>
					<input name="chip" type="radio" value="1" checked="checked" />有
					<input name="chip" type="radio" value="0" />无
				</td>
			</tr>
			
			<tr>
				<td>余额</td>
				<td>
					<input name="balance" value="22.33" />
				
				</td>
			</tr>
			
			<tr>
				<td>银行卡持有人</td>
				<td>
					<input name="owner" value="xq"  />
				
				</td>
			</tr>
			
			<tr>
				<td colspan="2">
					<input type="button"  onclick="addCard();" value="新增" />
				</td>
			</tr>
			
		</table>
		
		<script>
			
			
			
			
			function addCard(){
				
				
				//1 卡号的验证
				var cardNo = $('input[name="cardNo"]').val();
				
				if(''==cardNo){
					
					alert('卡号不能为空..');
					return false;
				}else{
					
					var rg=/^[0-9]{16}$/g;
					
					var jieguo = rg.test(cardNo);
					if(jieguo == false){
						alert('你输入的卡号不是16位的数字');
						return false;
					}
				}
				
				//2 做余额的验证....
				var balance  = $('input[name="balance"]').val();
				var bankId  = $('select[name="bankId"]').val();
				var chip  = $('input[name="chip"]').val();
				var owner  = $('input[name="owner"]').val();
				
				//3 发送请求 add
				$.ajax({
					async:true,
					data:{
						'method':'add',
						'cardNo':cardNo,
						'balance':balance,
						'bankId':bankId,
						'chip':chip,
						'owner':owner
					},
					url:getPath()+"/card",
					type:'post',
					dataType:'json',
					success:function(jsonData){
						
						var  code  = jsonData.result.code;
						
						if(code == '200'){
							
							//
							window.location.href="card-list.html"
							
							
						}else{
							alert("添加失败...");
						}
					}
				});
				
			}
			
			
			
			
			
			$(function(){
				
				//1 发送ajax请求获得所有的部门
				$.ajax({
					async:true,
					data:{
						'method':'findAll'
					},
					url:getPath()+"/bank",
					type:'post',
					dataType:'json',
					success:function(jsonData){
						
						var  code  = jsonData.result.code;
						
						if(code == '200'){
							
							//把银行要动态话
							
							//<option value="10">民生银行</option>
							//<option value="20">建设银行</option>
							
							//CTRL + K
							var bankId= $('#bankId');
							
							var data = jsonData.data;
							
							$(data).each(function(index,data){
								
								console.log(data);
								
								var valueField = data.valueField;
								var textField  = data.textField;
								
								var op = '<option value="'+valueField+'">'+textField+'</option>';
								console.log(op);
								
								bankId.append($(op));
								
							});
							
							
						}
					}
				});
				
				
				
			});
			
		</script>
	</body>
</html>